/*
 * @Date: 2023-08-07 15:40:22
 * @LastEditors: lyx
 * @LastEditTime: 2023-08-07 15:44:23
 * @FilePath: \sup-5t-mini-app\src\utils\dva.ts
 * @Description:
 */
import { create } from "dva-core";
import createLoading from "dva-loading";
import { createLogger } from "redux-logger";

let app: any;
let store: any;
let dispatch: any;
let registered: any;

function createApp(opt) {
  // redux日志
  opt.onAction = [];
  if (opt.enableLog) {
    opt.onAction.push(createLogger());
  }
  app = create(opt);
  app.use(createLoading());
  // 注入model
  if (!registered) {
    opt.models.forEach(model => app.model(model));
  }
  registered = true;
  app.start();
  // 设置store
  store = app._store;
  app.getStore = () => store;
  app.use({
    onError(err: any) {
      console.log(err);
    }
  });
  // 设置dispatch
  dispatch = store.dispatch;
  app.dispatch = dispatch;
  return app;
}

export default {
  createApp,
  getDispatch() {
    return app.dispatch;
  }
};
